import glob
import os
import time
from pathlib import Path
from 获取数据 import get_data
import concurrent.futures
from 合并excel import merge_excel_files
from datetime import datetime, timedelta
from dingding import SendFile
from login import login

# 账号
usernames1 = {'username': 'yexiong1997@qq.com', 'password': 'Yexiong1997', 'port': 9225, 'user_path': r"C:\15867710780",
              'down_path': r"C:\15867710780\filedownloads"}
usernames2 = {'username': '594688317@qq.com', 'password': 'Yh5566789', 'port': 9223, 'user_path': r"C:\594688317",
              'down_path': r"C:\594688317\filedownloads"}
usernames3 = {'username': '791002765@qq.com', 'password': 'yexiong1997Mm', 'port': 9224, 'user_path': r"C:\791002765",
              'down_path': r"C:\791002765\filedownloads"}
usernames4 = {'username': '2968425169@qq.com', 'password': 'yexiong1997Mm', 'port': 9226, 'user_path': r"C:\2968425169",
              'down_path': r"C:\2968425169\filedownloads"}
usernames5 = {'username': 'yexiong1108@qq.com', 'password': 'yexiong1997Mm', 'port': 9240,
              'user_path': r"C:\yexiong1108", 'down_path': r"C:\yexiong1108\filedownloads"}
usernames6 = {'username': 'y2968425169@foxmail.com', 'password': 'Yexiong1997', 'port': 9241,
              'user_path': r"C:\y2968425169", 'down_path': r"C:\y2968425169\filedownloads"}
usernames7 = {'username': 'yexiong1997@163.com', 'password': 'yexiong1997Mm', 'port': 9242,
              'user_path': r"C:\yexiong1997", 'down_path': r"C:\yexiong1997\filedownloads"}

if __name__ == '__main__':
    for i in range(1, 2):
        datetime = datetime.now() - timedelta(days=i+1)
        date = datetime.strftime("%Y-%m-%d")
        # 检查最终所需文件是否存在，如果存在则删除
        files = Path(f'C:/抖音客服数据/{date}').glob(f'*抖音客服数据{date}.xlsx')
        for file in files:
            file.unlink()
        # 检查是否登录
        login(usernames1)
        login(usernames2)
        login(usernames3)
        login(usernames4)
        login(usernames5)
        login(usernames6)
        login(usernames7)
        # 创建线程池
        with concurrent.futures.ThreadPoolExecutor() as executor:
            # 提交任务到线程池
            future1 = executor.submit(get_data, usernames1['port'], usernames1['user_path'],
                                      usernames1['down_path'], date,
                                      usernames1)
            future2 = executor.submit(get_data, usernames2['port'], usernames2['user_path'],
                                      usernames2['down_path'], date,
                                      usernames2)
            future3 = executor.submit(get_data, usernames3['port'], usernames3['user_path'],
                                      usernames3['down_path'], date,
                                      usernames3)
            future4 = executor.submit(get_data, usernames4['port'], usernames4['user_path'],
                                      usernames4['down_path'], date,
                                      usernames4)
            future5 = executor.submit(get_data, usernames5['port'], usernames5['user_path'],
                                      usernames5['down_path'], date,
                                      usernames5)
            future6 = executor.submit(get_data, usernames6['port'], usernames6['user_path'],
                                      usernames6['down_path'], date,
                                      usernames6)
            future7 = executor.submit(get_data, usernames7['port'], usernames7['user_path'],
                                      usernames7['down_path'], date,
                                      usernames7)
            concurrent.futures.wait([future1, future2, future3, future4, future5,  future6, future7])
            # 获取函数的返回值
            result1 = future1.result()
            result2 = future2.result()
            result3 = future3.result()
            result4 = future4.result()
            result5 = future5.result()
            result6 = future5.result()
            result7 = future7.result()
        if result1 == '成功' and result2 == '成功' and result3 == '成功' and result4 == '成功' and result5 == '成功' and result6 == '成功' and result7 == '成功':
            print('全部成功')
            input_files = glob.glob(os.path.join(f'C:\\抖音客服数据\\{date}', "*.xlsx"))
            num = len(input_files)
            output_file = f'C:\\抖音客服数据\\{date}\\{num}抖音客服数据{date}.xlsx'
            merge_excel_files(input_files, output_file)
            print('合并导出完成')
            SendFile(output_file)
        else:
            raise Exception('有店铺数据未获取成功，请检查')

